package com.wanshi.controller;

import com.wanshi.entity.Banner;
import com.wanshi.entity.PageBean;
import com.wanshi.entity.Route;
import com.wanshi.repository.BannerRepository;
import com.wanshi.repository.RouteRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

@Controller
@RequestMapping("/admin/banner")
public class BannerController {


    @Autowired
    private BannerRepository bannerRepository;

    @Autowired
    private RouteRepository routeRepository;



    @RequestMapping("/list")
    public String list(Model model,Integer page){

        List<Banner> bannerList = bannerRepository.findAll();
        List<Route> routeList = routeRepository.findAll();



        model.addAttribute("bannerList",bannerList);
        model.addAttribute("routeList",routeList);


        if (page==null){
            page=0;
        }
        Sort sort = Sort.by(Sort.Direction.DESC, "bannerId");
        Pageable pageable = PageRequest.of(page,1,sort);

        //Page<Banner> banners = bannerService.findAll(example, pageable);
        Page<Banner> banners = bannerRepository.findAll(pageable);

        List<Banner> list = banners.getContent();//本页数据列表
        long count = banners.getTotalElements();//总共多少条数据
        int pagenum = banners.getTotalPages();//多少页
        int dangqianye = page;

        PageBean pb = new PageBean(list,count,pagenum,dangqianye);
        model.addAttribute("pb", pb);



        return "/admin/banner/list";
    }

    @RequestMapping("/add")
    public String add(Model model){

        List<Route> routeList = routeRepository.findAll();
        model.addAttribute("routeList",routeList);
        return "/admin/banner/add";
    }

    @RequestMapping("/addHandle")
    public String addHandle(MultipartFile file , Banner banner) throws Exception {

        String filename = file.getOriginalFilename();
        String path = "C:/uploads/";
        File file1 = new File(path+filename);
        file.transferTo(file1);

        banner.setImgSrc(filename);

        bannerRepository.save(banner);

        return "redirect:/admin/banner/list";
    }

    @RequestMapping("/edit")
    public String edit(String bid, Model model){

        Banner banner = bannerRepository.getById(Integer.parseInt(bid));
        model.addAttribute("banner",banner);
        model.addAttribute("bid",bid);

        List<Route> routeList = routeRepository.findAll();
        model.addAttribute("routeList",routeList);

        return "/admin/banner/edit";
    }

    @RequestMapping("/update")
    public String update(MultipartFile file , Banner banner) throws Exception {

        if (!file.isEmpty()) {

            String filename = file.getOriginalFilename();
            String path = "C:/uploads/";

            //先删除图片
            File file2 =  new File("C:/uploads/"+banner.getImgSrc());
            if (file2.exists()){
                System.out.println("删除图片");
                file2.delete();
            }


            //再把新的图片赋值
            File file1 = new File(path+filename);
            file.transferTo(file1);
            banner.setImgSrc(filename);



        }

        bannerRepository.save(banner);

        return "redirect:/admin/banner/list";
    }

    @RequestMapping("/delete")
    public String delete(String bid){

        Banner banner = bannerRepository.getById(Integer.parseInt(bid));

        bannerRepository.deleteById(Integer.parseInt(bid));

        File file =  new File("C:/uploads/"+banner.getImgSrc());
        if (file.exists()){
            System.out.println("删除图片");
            file.delete();
        }

        return "redirect:/admin/banner/list";
    }


}
